package com.bwc.aidemo.paoqian



// TrainingDataParser.kt
object TrainingDataParser {
    // 解析原始数据（增强容错）
    fun parse(rawData: String): List<TrainingDetail> {
        val details = mutableListOf<TrainingDetail>()
        val lines = rawData.split("\n")
            .map { it.trim() }
            .filter { it.isNotEmpty() && it.contains("|") } // 过滤无效行

        lines.forEach { line ->
            try {
                val parts = line.split("|").map { it.trim() }  // 按照分隔符 "|" 分割字符串，并去掉多余空格
                if (parts.size == 6) {
                    val weekStr = parts[0].replace("第","").replace("周","")  // 取出第一部分，即 "第一周"
                    val weekNumStr = convertChineseNumber(weekStr)  // 转换中文数字
                    val week = weekNumStr.toIntOrNull() ?: return@forEach  // 转换为Int，如果失败则跳过该行


                    details.add(
                        TrainingDetail(
                            week = week,
                            day = parts[1],
                            type = parts[2],
                            distance = parts[3],
                            duration = parts[4],
                            note = parts[5]
                        )
                    )
                }
            } catch (e: Exception) {
                e.printStackTrace()  // 捕获并打印任何异常
            }
        }

        return details
    }

    // 生成每周数据（补全7天）
    fun generateWeeklyData(parsedDetails: List<TrainingDetail>): List<List<DailyData>> {
        val weekNumbers = parsedDetails.map { it.week }.distinct().sorted()
        val allDays = listOf("周一", "周二", "周三", "周四", "周五", "周六", "周日")
        return weekNumbers.map { week ->
            allDays.map { day ->
                val training = parsedDetails.find { it.week == week && it.day == day }
                DailyData(week, day, training)
            }
        }
    }



    private fun convertChineseNumber(chineseNumber: String): String {
        val chineseToNum = mapOf(
            "一" to "1", "二" to "2", "三" to "3", "四" to "4", "五" to "5",
            "六" to "6", "七" to "7", "八" to "8", "九" to "9", "十" to "10"
        )
        var result = chineseNumber
        for ((chinese, num) in chineseToNum) {
            result = result.replace(chinese, num)  // 将中文数字替换为阿拉伯数字
        }
        return result
    }
}